package com.tusuo.stds.util.webview;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.tusuo.stds.Constants;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.lab.LabActivity_;
import com.tusuo.stds.ui.main.MainActivity_;
import com.tusuo.stds.ui.main.PageDetailActivity_;
import com.tusuo.stds.ui.scenedetail.SceneDetailActivity_;

import org.parceler.Parcels;

/**
 * Created by developer on 17-9-14.
 */

public class FindWebViewClient extends BridgeWebViewClient {

    private Activity mActivity;

    public FindWebViewClient(BridgeWebView webView, Activity activity) {
        super(webView);
        mActivity = activity;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        Intent intent = new Intent();

        if (url.startsWith("stds://")) {
            url = url.replace("stds://", "");
            if (url.startsWith("lib/")) {
                intent.setClass(mActivity, LabActivity_.class);
                mActivity.startActivity(intent);
            }else if (url.startsWith("news/")) {
                if (mActivity instanceof MainActivity_) {
                    ((MainActivity_)mActivity).setCurrentTab(1);
                } else {
                    intent.setClass(mActivity,MainActivity_.class);
                    intent.putExtra("currentTab",1);
                    mActivity.startActivity(intent);
                    mActivity.finish();
                }
            }else if (url.startsWith("wrong/")) {
                if (mActivity instanceof MainActivity_) {
                    ((MainActivity_)mActivity).setCurrentTab(2);
                }else {
                    intent.setClass(mActivity,MainActivity_.class);
                    intent.putExtra("currentTab",2);
                    mActivity.startActivity(intent);
                    mActivity.finish();
                }
            } else if (url.startsWith("scene/")) {
                String sceneStr = url.substring(url.lastIndexOf("/") + 1);
                SceneList.Scene scene = new SceneList.Scene();
                scene.Id = Integer.valueOf(sceneStr);
                intent.setClass(mActivity, SceneDetailActivity_.class);
                intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(scene));
                mActivity.startActivity(intent);
//                mActivity.finish();
            } else if (url.startsWith("topic/")) {
                String topicId = url.substring(url.lastIndexOf("/") + 1);
                String topicUrl = Constants.WEB_PAGE_FIND_HOST + "cmsview/views/detail.html?id=" + topicId;
                intent.setClass(mActivity, PageDetailActivity_.class);
                intent.putExtra("url",topicUrl);
                mActivity.startActivity(intent);
            } else {
                if (mActivity instanceof MainActivity_) {
                    ((MainActivity_)mActivity).setCurrentTab(0);
                }else {
                    intent.setClass(mActivity,MainActivity_.class);
                    intent.putExtra("currentTab",0);
                    mActivity.startActivity(intent);
                    mActivity.finish();
                }
            }
            return true;
        } else if (url.startsWith("http://") || url.startsWith("https://")) {
            intent.setClass(mActivity, PageDetailActivity_.class);
            intent.putExtra("url",url);
            mActivity.startActivity(intent);
            return true;
        }

        return super.shouldOverrideUrlLoading(view, url);

    }
}
